/*
 * 文件名： WebViewActivity.java
 * 描    述：WEB页面 
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */

package com.miguo.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;

import com.miguo.activity.base.BaseWebViewActivity;
import com.miguo.framework.view.swipebacklayout.SwipeBackLayout;
import com.miguo.framework.web.CustomWebChromeClient;
import com.miguo.ui.R;
import com.miguo.utils.WebJsApi;

public class WebViewActivity extends BaseWebViewActivity implements
        OnClickListener {

    private static Activity mActivity;
    private static View mFooter;
    private static SwipeBackLayout mSwipeBackLayout;
    private String mLink;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        initView();
        initWebConfig();
        initWebView();
        initExtra();
    }

    private void initView() {
        mActivity = this;
        mFooter = findViewById(R.id.web_bottomLy);
        mSwipeBackLayout = getSwipeBackLayout();
    }

    private void initExtra() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            mBaseHead.setTitle(getResources().getString(R.string.app_name));
            mLink = getIntent().getStringExtra("url");
            mWebView.setWebChromeClient(new CustomWebChromeClient(this, "MeGuo", WebJsApi.class));
            mWebView.loadUrl(mLink);
        } else {
            onBackPressed();
        }
    }


    /**
     * JS回调设置标题
     *
     * @param title
     */
    public static void jsCallbackSetTitle(String title) {
        mBaseHead.setTitle(title);
    }

    /**
     * JS回调显示头部
     */
    public static void jsCallbackShowHeader() {
        mBaseHead.getView().setVisibility(View.VISIBLE);
    }

    /**
     * JS回调隐藏头部
     */
    public static void jsCallbackHideHeader() {
        mBaseHead.getView().setVisibility(View.GONE);
    }

    /**
     * JS回调显示底部
     */
    public static void jsCallbackShowFooter() {
        mFooter.setVisibility(View.VISIBLE);
    }

    /**
     * JS回调隐藏底部
     */
    public static void jsCallbackHideFooter() {
        mFooter.setVisibility(View.GONE);
    }

    /**
     * 滑屏退出设置
     *
     * @param enable
     */
    public static void jsCallbackSwipeBack(boolean enable) {
        mSwipeBackLayout.setEnableGesture(enable);
    }

    /**
     * 设置全屏模式
     */
    public static void jsCallbackFullScreen() {
        //设置无标题
        //mActivity.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置全屏
        mActivity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    /**
     * 退出全屏模式
     */
    public static void jsCallbackQuitFullScreen() {
        final WindowManager.LayoutParams attrs = mActivity.getWindow().getAttributes();
        attrs.flags &= (~WindowManager.LayoutParams.FLAG_FULLSCREEN);
        mActivity.getWindow().setAttributes(attrs);
        mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
    }

}
